package com.landking;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
import android.widget.Toast;

import com.landking.base.BaseActivity;
import com.landking.base.BaseFragment;
import com.landking.home.BookFragment;
import com.landking.home.FootFragment;
import com.landking.home.HotFragment;
import com.landking.home.MineFragment;
import com.landking.home.PicFragment;
import com.landking.view.convenientbanner.BannerCreator;

public class LandkingActivity extends BaseActivity {

    // 再点一次退出程序时间设置
    private static final long WAIT_TIME = 2000L;
    private long TOUCH_TIME = 0;

    private FragmentTabHost tabHost;
    private LayoutInflater layoutInflater;

    private Class fragmentArray[] = {HotFragment.class, PicFragment.class, BookFragment.class, FootFragment.class, MineFragment.class};
    private int imageViewArray[] = {R.drawable.selector_tab_image_hot, R.drawable.selector_tab_image_pic, R.drawable.selector_tab_image_book, R.drawable.selector_tab_image_foot, R.drawable.selector_tab_image_mine};
    private String tvArray[] = {"最火", "图片", "文章", "足迹", "我的"};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_landking);
        initView();
        initData();
    }

    private void initView() {
        tabHost = (FragmentTabHost) findViewById(R.id.tabhost);
    }

    private void initData() {
        int tabIndex = 0;
        Bundle bundle = getIntent().getBundleExtra("mainArgs");
        if (bundle != null) {
            tabIndex = bundle.getInt("tabIndex", 0);
        }

        layoutInflater = LayoutInflater.from(this);
        tabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        for (int i = 0; i < fragmentArray.length; i++) {
            TabSpec tabSpec = tabHost.newTabSpec(String.valueOf(i)).setIndicator(getTabItemView(i));
            tabHost.addTab(tabSpec, fragmentArray[i], bundle);
        }
        // 定位Tab
        tabHost.setCurrentTab(tabIndex);

    }

    /**
     * 获取每一个Tab标签显示内容
     *
     * @param index
     * @return
     */
    private View getTabItemView(int index) {
        View view = layoutInflater.inflate(R.layout.item_main_platform_tab, null);
        ImageView img = (ImageView) view.findViewById(R.id.tab_img);
        TextView title = (TextView) view.findViewById(R.id.tab_tv);
        img.setImageResource(imageViewArray[index]);
        title.setText(tvArray[index]);
        return view;
    }

    @Override
    public void onBackPressed() {
        final Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.realtabcontent);
        if (fragment != null && fragment instanceof BaseFragment && !((BaseFragment) fragment).allowBackPressed()) {
            if (System.currentTimeMillis() - TOUCH_TIME < WAIT_TIME) {
                finish();
            } else {
                TOUCH_TIME = System.currentTimeMillis();
                Toast.makeText(LandkingActivity.this, "再按一次返回键退出应用", Toast.LENGTH_SHORT).show();
            }
        }
    }




}
